package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/10 09:39
 **/
public class Day8_1_Num669_TrimBST {
    class Solution {
        public TreeNode trimBST(TreeNode root, int low, int high) {
            if(root == null) return root;
            // 当root.val < low，说明，该节点包括其左子树都小于最小范围，保留其右子树
            if(root.val < low) return trimBST(root.right, low, high);
            // 当root.val > high，说明，该节点包括其右子树都大于最大范围，保留其左子树
            if(root.val > high) return trimBST(root.left, low, high);

            root.left = trimBST(root.left, low, high);
            root.right = trimBST(root.right, low, high);
            return root;
        }
    }
}